package com.example.oasystem.controller.index;

import com.baomidou.mybatisplus.core.conditions.interfaces.Join;
import com.example.oasystem.dao.systemcontrol.SystemcontrolDao;
import com.example.oasystem.entity.attend.AoaAttendsList;
import com.example.oasystem.entity.daymanage.AoaScheduleList;
import com.example.oasystem.entity.daymanage.AoaScheduleListOfSun;
import com.example.oasystem.entity.notepaper.AoaNotepaperOfSun1;
import com.example.oasystem.entity.notice.AoaNoticeListOfSun;
import com.example.oasystem.entity.notice.NoticeDTO;
import com.example.oasystem.entity.plan.AoaPlanListOfSun;
import com.example.oasystem.entity.process.AoaNotepaperOfSun;
import com.example.oasystem.entity.process.AoaProcess;
import com.example.oasystem.entity.status.AoaStatusList;
import com.example.oasystem.entity.type.AoaTypeList;
import com.example.oasystem.entity.user.AoaUser;
import com.example.oasystem.service.AoaAttendsListService;
import com.example.oasystem.service.AoaTypeListService;
import com.example.oasystem.service.SystemcontrolService;

import com.example.oasystem.service.impl.SystemcontrolServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

@Controller
public class IndexController {

    @Autowired
    AoaAttendsListService aoaAttendsListService;

    @Autowired
    AoaTypeListService aoaTypeListService;

    @Autowired
    private SystemcontrolService systemcontrolService;
    @Autowired
    SystemcontrolDao systemcontrolDao;
    @Autowired
    SystemcontrolServiceImpl systemcontrolServiceImpl;


    @RequestMapping("/test2")
    public String index(HttpServletRequest request, Model model){
        AoaUser user = (AoaUser) request.getSession().getAttribute("user");
        Long userId = user.getUserId();
        int fileNum = systemcontrolService.selectFile(userId);
        int directorNum = systemcontrolService.selectDirector(userId);
        List<AoaTypeList> typeList = systemcontrolService.selectTypeList();
        List<AoaStatusList> statusList = systemcontrolService.selectStatusList();

        //公告通知
        getAoaNoticeListOfSuns(model, userId);

        //流程管理
        getAoaProcesses(model, userId);

        //工作计划
        getAoaPlanListOfSuns(model, userId);

        //我的便签
        getAoaNotepaper(model, userId);
        System.out.println("fileNum 111111111111111111111111111111111= " + fileNum);
        System.out.println("directorNum 1111111111111111111111111111111= " + directorNum);

        model.addAttribute("fileNum",fileNum);
        model.addAttribute("directorNum",directorNum);
        model.addAttribute("typeList",typeList);
        model.addAttribute("statusList",statusList);

        qiandao(request,model);

        //通知数量
        Integer notice = systemcontrolDao.getNotice(Math.toIntExact(user.getUserId()));
        model.addAttribute("notice",notice);

        //邮件数量
        Integer mail = systemcontrolDao.getMail(Math.toIntExact(user.getUserId()));
        model.addAttribute("mail",mail);

        //我的任务
        Integer task = systemcontrolDao.getTask(Math.toIntExact(user.getUserId()));
        model.addAttribute("task",task);
        return "systemcontrol/control";
    }

    //我的便签
    private void getAoaNotepaper(Model model, Long userId) {
        List<AoaNotepaperOfSun1> notepaperList=systemcontrolService.selectNotepaperList(userId,"");
        model.addAttribute("notepaperList",notepaperList);
    }

    @RequestMapping("/helpnotepaper")
    public String notepaper(Model model,HttpServletRequest request,@RequestParam(value = "concent",required = false,defaultValue = "")String concent){
        AoaUser user = (AoaUser) request.getSession().getAttribute("user");
        Long userId = user.getUserId();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = new Date();
        AoaNotepaperOfSun1 sun1 = new AoaNotepaperOfSun1();
        sun1.setConcent(concent);
        sun1.setCreateTime(sdf.format(date));
        sun1.setNotepaperUserId(userId);
        sun1.setTitle("我的便签");
        systemcontrolService.addnotepaper(sun1);
        List<AoaNotepaperOfSun1> notepaperList=systemcontrolService.selectNotepaperList(userId,concent);
        model.addAttribute("notepaperList",notepaperList);
        return "systemcontrol/notepaper";
    }

    //工作计划
    private void getAoaPlanListOfSuns(Model model, Long userId) {
        List<AoaPlanListOfSun> planList = systemcontrolService.selectPlan(userId);
        model.addAttribute("planList",planList);
    }



    //流程管理
    private void getAoaProcesses(Model model, Long userId) {
        List<AoaProcess> processlist = systemcontrolService.selectProcess(userId);
        model.addAttribute("processlist",processlist);

    }

    //公告通知
    private void getAoaNoticeListOfSuns(Model model, Long userId) {
        List<AoaNoticeListOfSun> noticeList = systemcontrolService.selectNotice(userId);
        model.addAttribute("noticeList",noticeList);

    }

    //日程管理

    //日历的数据显示
    @RequestMapping("littlecalendar")
    @ResponseBody
    public List test3df(HttpServletRequest request,Model model) throws IOException {
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
        AoaUser user = (AoaUser) request.getSession().getAttribute("user");
        Long userId = user.getUserId();
        List list=new ArrayList<>();
        List<AoaScheduleListOfSun>  dates=systemcontrolService.findstart(userId);
        for (AoaScheduleListOfSun date : dates) {

            try {
                list.add(sdf.format(sdf.parse(date.getStartTime())));
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }
        return list;
    }

    public void  qiandao(HttpServletRequest request, Model model)
    {
        AoaUser user = (AoaUser)request.getSession().getAttribute("user");
        Long attanendsUserId = user.getUserId();
        //时间格式化
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = new Date();
        // 截取当天时间
        SimpleDateFormat sdf4 = new SimpleDateFormat("yyyy-MM-dd");
        // 截取时分秒
        SimpleDateFormat sdf3 = new SimpleDateFormat("HH:mm:ss");
        // 一年当中的年月日
        String nowdata = sdf4.format(date);
        //用户当前的打卡状态
        List<AoaAttendsList> aoaAttendsLists = aoaAttendsListService.AoaAttendsList(attanendsUserId,nowdata);
        if (aoaAttendsLists.size()>0){
            //得到当前上下班状态
            Long typeId = 9L;
            String type = aoaTypeListService.typeName(typeId);
            model.addAttribute("type",type);
            if (aoaAttendsLists.size()==1){
                AoaAttendsList aoaAttendsList = aoaAttendsLists.get(0);
                model.addAttribute("alist",aoaAttendsList);
            }
            if (aoaAttendsLists.size()==2){
                AoaAttendsList aoaAttendsList = aoaAttendsLists.get(1);
                model.addAttribute("alist",aoaAttendsList);
            }
        }
    }
    //使用统计
    @ResponseBody
    @RequestMapping("countweeklogin")
    public List<Integer> countweeklogin(HttpSession session){
        List<Integer> list = new ArrayList<>();
        String[] date=getDate();
        for (String s : date) {
            int tian=systemcontrolDao.getLoginCount(s);
            list.add(tian);
        }
        return list;
    }
    public String[] getDate()
    {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Calendar calendar = Calendar.getInstance();
        while (calendar.get(Calendar.DAY_OF_WEEK) != Calendar.MONDAY) {
            calendar.add(Calendar.DAY_OF_WEEK, -1);
        }
        String[] dates = new String[7];
        for (int i = 0; i < 7; i++) {
            dates[i] = dateFormat.format(calendar.getTime());
            calendar.add(Calendar.DATE, 1);
        }
        return dates;
    }
    //任务完成统计
    @ResponseBody
    @RequestMapping("counttasknum")
    public List<String> counttasknum(HttpSession session){
        AoaUser user = (AoaUser)session.getAttribute("user");
        int userId = Math.toIntExact(user.getUserId());
        List<String> userByName = systemcontrolServiceImpl.getUserByName();
        return userByName;
    }



}
